using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class RolDao : DaoBase
    {
        public RolDao(string strConn) : base(strConn)
        {
        }

        RolHelper helper = new RolHelper();

        public int Save(Rol entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, entity.ID);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.DesDescripcion, DbType.String, entity.DesDescripcion);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);

            return (int)dbProvider.ExecuteScalar(command);
        }

        public void Update(Rol entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, entity.ID);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.DesDescripcion, DbType.String, entity.DesDescripcion);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(System.Int32 id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, id);

            dbProvider.ExecuteNonQuery(command);
        }

        public Rol GetById(System.Int32 id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, id);
            Rol entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<Rol> List()
        {
            List<Rol> entitys = new List<Rol>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Rol> GetByCriteria(string nombre)
        {
            List<Rol> entitys = new List<Rol>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, nombre);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }
    }
}
